package com.guchenbo.scala.c6

/**
 * @author guchenbo
 * @date 2022/3/19
 */
object MatchDemo {

  def main(args: Array[String]): Unit = {
    val stu = new Stu("jay", 18)
    val str = stu match {
      case Stu("jay", 18) => "match"
      case _ => "none"
    }
    println(str)

    val st2 = Stu2("jay",18) match {
      case Stu2("jay",18) => "match"
      case _=> "none"
    }
    println(st2)
  }
}

class Stu(var name: String, var age: Int) {

}

object Stu {
  /**
   * 把stu对象的拆开成内容
   *
   * @param stu
   * @return
   */
  def unapply(stu: Stu): Option[(String, Int)] = {
    if (stu == null) {
      None
    } else {
      Some((stu.name, stu.age))
    }
  }

  def apply(name: String, age: Int): Stu = new Stu(name, age)
}

case class Stu2(name:String, age :Int){

}